﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreadModule.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ThreadModule
{
    [TestClass]
    public class ThreadSolver
    {
        /// <summary>
        /// 上半部分数据点集
        /// </summary>
        public List<RawDataPoint> listTopRawData { get; set; }

        /// <summary>
        /// 下半部分数据点集
        /// </summary>
        public List<RawDataPoint> listBottomRawData { get; set; }

        public ThreadSolver()
        {

        }

        public ThreadSolver(List<RawDataPoint> listTopRawData, List<RawDataPoint> listBottomRawData)
        {
            this.listTopRawData = listTopRawData;
            this.listBottomRawData = listBottomRawData;
        }

        [TestMethod]
        public void TestTaperThread()
        {
            Initialize("ZT5647_圆锥螺纹塞规_2-11 1-2 NPT.txt", "ZT5647_圆锥螺纹塞规_2-11 1-2 NPT_下.txt");
            //listTopRawData = listTopRawData.GetRange(1000, 15000);
            //listBottomRawData = listBottomRawData.GetRange(1000, 15000);
            ThreadInformation threadInformation = new ThreadInformation();
            threadInformation.guageType = ThreadGaugeType.CylinderPlug;
            ThreadParameters threadNominalParameters = new ThreadParameters();
            threadNominalParameters.isNominalValues = true;
            threadNominalParameters.pitch = 25.4/11.5;
            threadNominalParameters.pitchDiameter = 58.325;
            threadNominalParameters.fullAngle = 60.0;
            threadNominalParameters.leftHalfAngle = 30.0;
            threadNominalParameters.rightHalfAngle = 30.0;
            threadNominalParameters.taper = 1.0 / 16.0;
            ThreadRawData topThreadRawData = new ThreadRawData();
            ThreadRawData bottomThreadRawData = new ThreadRawData();
            topThreadRawData.listRawData = listTopRawData;
            topThreadRawData.position = Position.Top;
            bottomThreadRawData.listRawData = listBottomRawData;
            bottomThreadRawData.position = Position.Bottom;

            threadInformation.nominalThreadParameter = threadNominalParameters;
            threadInformation.bottomThreadRawData = bottomThreadRawData;
            threadInformation.topThreadRawData = topThreadRawData;

            ThreadBuilder<TaperThreadGauge> threadBuilder = new ThreadBuilder<TaperThreadGauge>(ref threadInformation);
        }

        [TestMethod]
        public void TestCylinderThread()
        {
            Initialize("ZT5392_螺纹塞规_M20×2.5-6H T.txt", "ZT5392_螺纹塞规_M20×2.5-6H T_下.txt");
            //listTopRawData = listTopRawData.GetRange(1000, 15000);
            //listBottomRawData = listBottomRawData.GetRange(1000, 15000);
            ThreadInformation threadInformation = new ThreadInformation();
            threadInformation.guageType = ThreadGaugeType.CylinderPlug;
            ThreadParameters threadNominalParameters = new ThreadParameters();
            threadNominalParameters.isNominalValues = true;
            threadNominalParameters.pitch = 2.5;
            threadNominalParameters.pitchDiameter = 20.0;
            threadNominalParameters.fullAngle = 60.0;
            threadNominalParameters.leftHalfAngle = 30.0;
            threadNominalParameters.rightHalfAngle = 30.0;
            ThreadRawData topThreadRawData = new ThreadRawData();
            ThreadRawData bottomThreadRawData = new ThreadRawData();
            topThreadRawData.listRawData = listTopRawData;
            topThreadRawData.position = Position.Top;
            bottomThreadRawData.listRawData = listBottomRawData;
            bottomThreadRawData.position = Position.Bottom;

            threadInformation.nominalThreadParameter = threadNominalParameters;
            threadInformation.bottomThreadRawData = bottomThreadRawData;
            threadInformation.topThreadRawData = topThreadRawData;

            ThreadBuilder<CylinderThreadGauge> threadBuilder = new ThreadBuilder<CylinderThreadGauge>(ref threadInformation);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        [TestMethod]
        public void Initialize(string upFileName, string bottomFileName)
        {
            //string fileName = "ZT5392_螺纹塞规_M20×2.5-6H T.txt";//ZT5645_螺纹塞规_M36×4-6H T_下.txt //ZT5647_圆锥螺纹塞规_2-11 1-2 NPT.txt
            string path = $"{Directory.GetCurrentDirectory()}\\TestData\\{upFileName}";
            string[] strLines = File.ReadAllLines(path);

            listTopRawData = new List<RawDataPoint>();
            for (int i = 0; i < strLines.Length; i++)
            {
                var strValues = strLines[i].Split('\t');
                double X = Convert.ToDouble(strValues[0]);
                double Y = Convert.ToDouble(strValues[1]);
                listTopRawData.Add(new RawDataPoint() { X = X, Y = Y });
            }

            //bottomFileName = "ZT5392_螺纹塞规_M20×2.5-6H T_下.txt";//ZT5645_螺纹塞规_M36×4-6H T_下.txt //ZT5647_圆锥螺纹塞规_2-11 1-2 NPT_下.txt
            path = $"{Directory.GetCurrentDirectory()}\\TestData\\{bottomFileName}";
            strLines = File.ReadAllLines(path);

            listBottomRawData = new List<RawDataPoint>();
            for (int i = 0; i < strLines.Length; i++)
            {
                var strValues = strLines[i].Split('\t');
                double X = Convert.ToDouble(strValues[0]);
                double Y = Convert.ToDouble(strValues[1]);
                listBottomRawData.Add(new RawDataPoint() { X = X, Y = Y });
            }
        }

        public static List<RawDataPoint> GetRawPoints(string fileName)
        {
            string path = $"{Directory.GetCurrentDirectory()}\\TestData\\{fileName}";
            string[] strLines = File.ReadAllLines(path);

            var listRawData = new List<RawDataPoint>();
            for (int i = 0; i < strLines.Length; i++)
            {
                var strValues = strLines[i].Split('\t');
                double X = Convert.ToDouble(strValues[0]);
                double Y = Convert.ToDouble(strValues[1]);
                listRawData.Add(new RawDataPoint() { X = X, Y = Y });
            }
            return listRawData;
        }
    }
}
